package com.nus.fellowmark.modules.module.service.impl;

import com.nus.fellowmark.dao.EnrollmentDao;
import com.nus.fellowmark.entity.EnrollmentEntity;
import com.nus.fellowmark.modules.module.service.EnrollmentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;


@Service("enrollmentService")
public class EnrollmentServiceImpl extends ServiceImpl<EnrollmentDao, EnrollmentEntity> implements EnrollmentService {

    @Autowired
    EnrollmentDao enrollmentDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<EnrollmentEntity> page = this.page(
                new Query<EnrollmentEntity>().getPage(params),
                new QueryWrapper<EnrollmentEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Integer> getStudentIdsInModule(Integer moduleId) {
        List<EnrollmentEntity> enrollmentEntities = this.list(new QueryWrapper<EnrollmentEntity>().eq("module_id", moduleId));
        return enrollmentEntities.stream().map(EnrollmentEntity::getStudentId).collect(Collectors.toList());
    }

    @Override
    public List<EnrollmentEntity> getByStudentId(Integer studentId) {
        List<EnrollmentEntity> enrollment = enrollmentDao.selectByStudentId(studentId);
        return enrollment;
    }

    @Override
    public EnrollmentEntity getByStudentIdAndModuleId(Integer studentId, Integer moduleId) {
        EnrollmentEntity enrollment = enrollmentDao.selectByStudentIdAndModuleId(studentId, moduleId);
        return enrollment;
    }

}
